<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="like_contactName">
        <el-input
          v-model="queryParams.like_contactName"
          placeholder="请输入姓名"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="物流状态" prop="status">
        <el-select
          v-model="queryParams.logistics_status"
          placeholder="物流状态"
          clearable
          size="small"
          style="width: 140px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="datePick"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="modifyInfoAll"
        >更新物流</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['system:role:export']"-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          :loading="exportLoading"-->
      <!--          @click="handleExport"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <!--      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />-->
    </el-row>

    <el-table v-loading="loading" :data="list" style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" :selectable="checkSelectable" width="55" />
      <!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="订单日期" align="center" prop="orderDate" />
      <el-table-column label="收货人" align="center" prop="contactName" />
      <el-table-column label="收货地址" align="center">
        <template v-slot="scope">
          {{ scope.row.contactAddr + " " + scope.row.detailAddr }}
        </template>
      </el-table-column>
      <el-table-column label="物流公司" align="center" prop="express_company" />
      <el-table-column label="物流单号" align="center" prop="express_no" />
      <el-table-column label="推送状态" align="center" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="primary"
            @click="see(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="success"
            :disabled="scope.row.logistics_status === 1 ? false : true"
            @click="modifyInfoOne(scope.row.orderId)"
          >更新物流</el-button>
        </template>

      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 查看详情 -->
    <LogisticsDetails :show.sync="show" :form="form" />
  </div>
</template>
<script>
import { list, modifyInfo } from '@/api/logistics'
import { orderInfo } from '@/api/order'
export default {
  name: 'Order',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 日期范围
      dateRange: [],
      statusOptions: [
        { label: '未推送', value: 0 },
        { label: '已完成', value: 1 },
        { label: '已推送', value: 2 },
        { label: '已失败', value: 3 },
        { label: '已加入', value: 4 }
      ],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        orderId: undefined,
        like_contactName: undefined,
        logistics_status: undefined,
        start_orderDate: undefined,
        end_orderDate: undefined
      },
      ids: [],
      show: false,
      form: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true
      list(this.queryParams).then(response => {
        if (response.code === 10000) {
          this.list = response.data
          this.total = response.count
          this.loading = false
        }
      })
    },
    /** 查询订单详情 */
    see(row) {
      orderInfo({ orderId: row.orderId, type: 1 }).then(response => {
        if (response.code === 10000) {
          this.form = response.data
          this.show = true
        }
      })
    },
    /** 状态字典翻译 */
    statusFormat(row, column) {
      let status = row.logistics_status
      for (const d of this.statusOptions) {
        if (row.logistics_status === d.value) {
          status = d.label
          break
        }
      }
      return status
    },
    /** 查询订单列表 */
    modifyInfoOne(orderId) {
      const that = this
      // console.log(data)
      that.$confirm('是否更新' + orderId + '物流信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        modifyInfo({ orders: that.ids }).then(response => {
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          }
          that.loading = false
        })
      }).catch(() => {})
    },
    /** 更新多单信息 */
    modifyInfoAll() {
      const that = this

      if (that.ids.length === 0) {
        that.$message.error('请选择订单')
        return
      }
      // console.log(data)
      that.$confirm('是否更新' + that.ids[0] + '...等物流信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        modifyInfo({ orders: that.ids }).then(response => {
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          }
          that.loading = false
        })
      }).catch(() => {})
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams = {
        page: 1,
        limit: 10,
        orderId: undefined,
        like_contactName: undefined,
        logistics_status: undefined,
        start_orderDate: undefined,
        end_orderDate: undefined
      }
      this.handleQuery()
    },
    /** 提交时间改变 */
    datePick(value) {
      if (value) {
        this.queryParams.start_orderDate = value[0]
        this.queryParams.end_orderDate = value[1]
      } else {
        this.queryParams.start_orderDate = undefined
        this.queryParams.end_orderDate = undefined
      }
    },
    /** 禁止选中的项 */
    checkSelectable(row) {
      return row.logistics_status === 1
    },
    /** 选中的项 */
    handleSelectionChange(val) {
      const ids = []
      val.map((item, index, val) => {
        ids.push(item.orderId)
      })
      this.ids = ids
    }
  }
}
</script>
